import type { BaseInterface } from "./index";
import { DB_Query_UserInfo } from "@/api/user.server";

export namespace User {
  export interface Info extends BaseInterface {
    nickname?: string;
    username: string;
    avatar_id?: number;
    /** 性别 0表示未知 1表示男 2表示女 */
    sex?: 0 | 1 | 2;
    /** 个人简介 */
    introduction?: string;
    email?: string;
    phone?: string;
    /** 1启用 2禁用 */
    status: 0 | 1;
  }
}

export class User implements User.Info {
  id!: number;
  nickname?: string;
  username!: string;
  avatar_id?: number;
  sex?: 0 | 1 | 2 = 0;
  introduction?: string;
  email?: string;
  phone?: string;
  status: 0 | 1 = 1;
  create_user?: number;
  update_user?: number;
  created_at?: string;
  updated_at?: string;

  constructor(params: User.Info) {
    Object.assign(this, params);
  }
  /** 获取用户列表 */
  static async getUserList() {
    const result = await DB_Query_UserInfo();
    return { ...result, data: result.data.map(item => new User(item)) };
  }
  /** 获取用户信息 */
  static async getUserInfo(id: number) {
    const result = await DB_Query_UserInfo(id);
    return { ...result, data: result.data.map(item => new User(item))[0] };
  }
}
